/*
 * Date          Author      Note
 * 2014.12.01    舞者无罪           文件存储
 * 2015.06.24    wzwz       增加返回按钮
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "io.h"
#include "com.h"
#include "sys_input.h"
#include "work_input.h"
#include "file_out.h"
#include "file_input.h"
#include "sys_output.h"
#include "maint.h"
#include "password.h"
#include "info.h"
#include "log.h"

#define FILE_CAPTION_LEFT    (30)
#define FILE_CAPTION_TOP     (60)
#define FILE_CAPTION_WIDTH   (200)
#define FILE_CAPTION_HEIGHT  (30)
/*
 *
 */
static void CreateFileIOInterface(HWND hWnd)
{
    CreateWindow (MH_BTN_NAME,
        MW_FILE_INPUT1_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_FILE_1IO,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP, FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_PARAM_INPUT2_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_FILE_2IO,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP + FILE_CAPTION_HEIGHT,
        FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_PARAM_INPUT3_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_FILE_3IO,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP + FILE_CAPTION_HEIGHT * 2,
        FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_FILE_OUTPUT4_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_FILE_4IO,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP + FILE_CAPTION_HEIGHT * 3,
        FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_PARAM_OUTPUT5_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_FILE_5IO,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP + FILE_CAPTION_HEIGHT * 4,
        FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_PARAM_OUTPUT6_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_FILE_6IO,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP + FILE_CAPTION_HEIGHT * 5,
        FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_MAC_MAINT7_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_FILE_7IO,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP + FILE_CAPTION_HEIGHT * 6,
        FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_LOG_OUT8_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_FILE_8IO,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP + FILE_CAPTION_HEIGHT * 7,
        FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    char esc[64] = {0};
    sprintf(esc, GetString(802), 9);
    CreateWindow(MH_BTN_NAME,
        esc,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_ESC,
        30,
        60 + 30 * 8,
        200,
        30,
        hWnd, 0);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_ESC:
        {
            PostMessage (hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case ID_FILE_1IO:
        {
            if(HasUsb())
            {
                CreateFileInputWindow(hWnd);
            }
            else
            {
                AfxInfoBox(hWnd, NULL, GetString(555));
            }
        }
        break;
    case ID_FILE_2IO:
        {
            if(HasUsb())
            {
                CreateSysInputWindow(hWnd);
            }
            else
            {
                AfxInfoBox(hWnd, NULL, GetString(555));
            }
        }
        break;
    case ID_FILE_3IO:
        {
             if(HasUsb())
            {
                 CreateWorkInputWindow(hWnd);
            }
            else
            {
                AfxInfoBox(hWnd, NULL, GetString(555));
            }
        }
        break;
    case ID_FILE_4IO:
        {
             if(HasUsb())
            {
                 CreateFileOutWindow(hWnd);
            }
            else
            {
                AfxInfoBox(hWnd, NULL, GetString(555));
            }
        }
        break;
    case ID_FILE_5IO:
        if(HasUsb())
        {
            HWND hDlg = CreatePasswordDialog(hWnd);
            SetWindowCaption(hDlg, MW_INPUT_NAME_CAPTION);
            SetPasswordOtherInput(hDlg, TRUE);
            if(PasswordDoModal(hDlg))
            {
                int len = strlen(GetUsbDir()) + strlen(g_edit_text);
                len += sizeof(MAC_FILE_SUFFIX);
                char *to = calloc(1, len);
                strcat(to, GetUsbDir());
                strcat(to, g_edit_text);
                strcat(to, MAC_FILE_SUFFIX);
                //int ret = CopySysParamFile(MAC_PARAM_FILE, to);
                int ret = WriteMacFile(to, g_knit_data);
                if(ret)
                {
                    printf("failed to copy mac file: %s\n", to);
                    AddSysLog(g_knit_data, LOG_TYPE_FILE, ret);
                    AfxInfoBox(hWnd, NULL, GetString(481));
                }
                else
                {
                    MH_FILE_SYNC();
                    AfxInfoBox(hWnd, NULL, GetString(483));
                }
                FREE(to);
            }
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(555));
        }
        break;
    case ID_FILE_6IO:
        if(HasUsb())
        {
            char *p = GetKnitFileName(g_knit_data);
            int from_len = sizeof(KNIT_FILE_DIR) + strlen(p);
            from_len += sizeof(SPEED_FILE_SUFFIX);
            char *from = calloc(1, from_len);
            strcat(from, KNIT_FILE_DIR);
            strcat(from, p);
            strcat(from, SPEED_FILE_SUFFIX);

            int to_len = strlen(GetUsbDir()) + strlen(p) + sizeof(SPEED_FILE_SUFFIX);
            char *to = calloc(1, to_len);
            strcat(to, GetUsbDir());
            strcat(to, p);
            strcat(to, SPEED_FILE_SUFFIX);
            int ret = CopySpeedFile(from, to);
            if(ret)
            {
                printf("failed to copy speed file: %s\n", to);
                AddSysLog(g_knit_data, LOG_TYPE_FILE, ret);
                AfxInfoBox(hWnd, NULL, GetString(481));
            }

            memset(from, 0, from_len);
            strcat(from, KNIT_FILE_DIR);
            strcat(from, p);
            strcat(from, DUMU_FILE_SUFFIX);

            to_len = strlen(GetUsbDir()) + strlen(p) + sizeof(DUMU_FILE_SUFFIX);
            memset(to, 0, to_len);
            strcat(to, GetUsbDir());
            strcat(to, p);
            strcat(to, DUMU_FILE_SUFFIX);

            ret = CopyDumuFile(from, to);
            if(ret)
            {
                printf("failed to copy dumu file: %s\n", to);
                AddSysLog(g_knit_data, LOG_TYPE_FILE, ret);
                AfxInfoBox(hWnd, NULL, GetString(481));
            }

            memset(from, 0, from_len);
            strcat(from, KNIT_FILE_DIR);
            strcat(from, p);
            strcat(from, STOP_FILE_SUFFIX);

            to_len = strlen(GetUsbDir()) + strlen(p) + sizeof(STOP_FILE_SUFFIX);
            memset(to, 0, to_len);
            strcat(to, GetUsbDir());
            strcat(to, p);
            strcat(to, STOP_FILE_SUFFIX);

             ret = CopyStopFile(from, to);
            if(ret)
            {
                printf("failed to copy yarn stop pos file: %s\n", to);
                AddSysLog(g_knit_data, LOG_TYPE_FILE, ret);
                AfxInfoBox(hWnd, NULL, GetString(481));
            }
            else
            {
                AfxInfoBox(hWnd, NULL, GetString(483));
            }


            FREE(from);
            FREE(to);
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(555));
        }
        break;
    case ID_FILE_7IO:
        {
            CreateMaintWindow(hWnd);
        }
        break;
    case ID_FILE_8IO:
        {
            CreateLogWindow(hWnd);
        }
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE || key == SCANCODE_9)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_1)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_FILE_1IO));
        CreateFileInputWindow(hWnd);
    }
    else if(key == SCANCODE_2)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_FILE_2IO));
        CreateSysInputWindow(hWnd);
    }
    else if(key == SCANCODE_3)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_FILE_3IO));
        CreateWorkInputWindow(hWnd);
    }
    else if(key == SCANCODE_4)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_FILE_4IO));
        CreateFileOutWindow(hWnd);
    }
    else if(key == SCANCODE_5)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_FILE_5IO));
        OnBtnCmd(hWnd, ID_FILE_5IO);
    }
    else if(key == SCANCODE_6)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_FILE_6IO));
        OnBtnCmd(hWnd, ID_FILE_6IO);
    }
    else if(key == SCANCODE_7)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_FILE_7IO));
        OnBtnCmd(hWnd, ID_FILE_7IO);
    }
    else if(key == SCANCODE_8)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_FILE_8IO));
        OnBtnCmd(hWnd, ID_FILE_8IO);
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID == ID_FILE_8IO)
        {
            nID = ID_ESC;
        }
        else if(nID != ID_ESC)
        {
            nID++;;
        }
        SetFocusChild(GetDlgItem(hWnd, nID));
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID == ID_ESC)
        {
            nID = ID_FILE_8IO;
        }
        else if(nID != ID_FILE_1IO)
        {
             nID--;
        }
        SetFocusChild(GetDlgItem(hWnd, nID));
    }
    else if(key == SCANCODE_ENTER)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        OnBtnCmd(hWnd, nID);
    }
}
/*
 *
 */
static int FileIOWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateFileIOInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup (hWnd);
        }
        return 0;
    case MSG_DESTROY:
        ShowWindow(hWnd, SW_HIDE);
        DestroyAllControls (hWnd);
        break;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_MAST]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
*
*/
HWND CreateIOWindow(HWND hHosting)
{
    int x = (CX_SCREEN - MASTER_WND_CX) / 2;
    int y = (CY_SCREEN - MASTER_WND_CY) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_FILE_IO_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = FileIOWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + MASTER_WND_CX;
    CreateInfo.by = y + MASTER_WND_CY;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, ID_FILE_1IO));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
